package com.example.tianling.admin.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.tianling.admin.params.deleteListBookDo;
import com.example.tianling.admin.service.BookManageService;
import com.example.tianling.admin.vo.BookDataVo;
import com.example.tianling.common.annotation.Login;
import com.example.tianling.common.utils.R;
import com.example.tianling.entity.BookEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
public class BookManagerController {
    //根据id 返回对应的数据
    @Autowired
    private BookManageService bookService ;

    @Login
    @PostMapping("/admin/searchBook")
    @ResponseBody
    public R searchBookController(@RequestBody String params) {
        //这个是搜索文章，下一页也在这里
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String keyword = paramsJSONObject.getString("keyword");
        String type = paramsJSONObject.getString("type");
        String page = paramsJSONObject.getString("page");
        Page<BookEntity> bookServicePage =  bookService.searchBook(Integer.valueOf(page), Integer.valueOf(type),keyword);
        return R.success(bookServicePage);
    }
    @Login
    @PostMapping("/admin/detail")
    @ResponseBody
    public R detailBookController(@RequestBody String params) {
        //这个是文章详情
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String bookId = paramsJSONObject.getString("bookId");
        BookEntity bookEntity = bookService.detailBook(bookId);
        return R.success(bookEntity);
    }
    @Login
    @PostMapping("/admin/updataBook")
    @ResponseBody
    public R updataBookController(@RequestBody String params) {
        //这个是文章详情
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String bookId = paramsJSONObject.getString("bookId");
        String bookName = paramsJSONObject.getString("bookName");
        String year = paramsJSONObject.getString("year");
        String area = paramsJSONObject.getString("area");
        String bookType = paramsJSONObject.getString("bookType");
        String content = paramsJSONObject.getString("content");
        int result = bookService.updataBook( bookId, bookName, year, area, bookType, content);
        return R.success(result);
    }
    @Login
    @PostMapping("/admin/deleteBook")
    @ResponseBody
    public R deldeteBookController(@RequestBody deleteListBookDo deleteListBookDo) {
        //这个是删除
        int result = bookService.deleteBook( deleteListBookDo.getDeleteList());
        return R.success(result);
    }
    @Login
    @PostMapping("/admin/insertBook")
    @ResponseBody
    public R insertBookController( @RequestBody String params) {
        //这个是上传
        JSONObject paramsJSONObject = JSONObject.parseObject(params);
        String bookName = paramsJSONObject.getString("bookName");
        String year = paramsJSONObject.getString("year");
        String area = paramsJSONObject.getString("area");
        String bookType = paramsJSONObject.getString("bookType");
        String content = paramsJSONObject.getString("content");
        int result = bookService.insertBook( bookName, year, area, bookType, content);
        return R.success(result);
    }
    @Login
    @PostMapping("/admin/loadData")
    @ResponseBody
    public R loadDataController() {
        //这个是数据
        BookDataVo result = bookService.loadData();
//        System.out.println(result);

        return R.success(result);
    }
}
